package org.airline.impl;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;

import org.airline.dao.IHibernateDao;
import org.airline.entity.PageBean;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Component;



/**
 * 项目名称：OrderTicketSys
 * 创   建  人：曾显圻
 * 创建时间：2011-11-10 下午12:31:52
 * 包   	名  ：org.order.impl
 * 类  名  称 ：HibernateDaoImpl.java
 * 类  描  述 : HibernateDaoImpl底层实现类 
 */
@Component("HibernateDaoImpl")
@SuppressWarnings("all")
public class HibernateDaoImpl extends SuperHibernateTemplate implements IHibernateDao{

	public void delete(Object object) throws Exception {
		super.getHibernateTemplate().delete(object);
		
	}

	public void delete(Class clazz, Serializable id) throws Exception {
		super.getHibernateTemplate().delete(load(clazz, id));
		
	}

	public Integer deleteAll(final Class clazz) throws Exception {
		return (Integer) super.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session) {
						Query query = session.createQuery("delete "
								+ clazz.getName());
						return new Integer(query.executeUpdate());
					}
				});
		
	}

	public void deleteByQuery(final String queryString, final Object[] parameters)
			throws Exception {
		super.getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session){
				Query query = session.createQuery(queryString);
				if (parameters != null){
					for(int i=0;i<parameters.length;i++){
						query.setParameter(i, parameters[i]);
					}
				}
				return new Integer(query.executeUpdate());
			}
		});
	}

	public List find(String queryString) throws Exception {
		return super.getHibernateTemplate().find(queryString);
		
	}

	public List find(String queryString, Object[] parameters) throws Exception {
		return super.getHibernateTemplate().find(queryString, parameters);
	}

	public List findAll(Class clazz) throws Exception {
		
		return super.getHibernateTemplate().find("from"+clazz.getName());
	}

	public List findByNameQuery(String queryName) throws Exception {
		
		return super.getHibernateTemplate().findByNamedQuery(queryName);
	}

	public List findByNameQuery(String queryName, Object[] parameters)
			throws Exception {
		
		return super.getHibernateTemplate().findByNamedQuery(queryName, parameters);
	}

	public List findByNameQuery(String queryName, Object parameters)
			throws Exception {

		return super.getHibernateTemplate().findByNamedQuery(queryName, parameters);
	}

	public Object get(Class clazz, Serializable id) throws Exception {
		
		return super.getHibernateTemplate().get(clazz, id);
	}

	public Object get(String queryString, Object[] parameters) throws Exception {
		List list = super.getHibernateTemplate().find(queryString, parameters);
		if(list != null && ! list.isEmpty()){
			return list.get(0);
		}
		return null;
	}

	public Object load(Class clazz, Serializable id) throws Exception {
		
		return super.getHibernateTemplate().load(clazz, id);
	}

	public void save(Object object) throws Exception {
		super.getHibernateTemplate().save(object);
		
	}

	public void update(Object object) throws Exception {
		super.getHibernateTemplate().update(object);
		
	}

	public void updateByQuery(final String queryString, final Object[] parameters)
			throws Exception {
		super.getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session){
				Query query = session.createQuery(queryString);
				if (parameters != null) {
					for (int i = 0; i < parameters.length; i++) {
						query.setParameter(i, parameters[i]);
					}
				}
				query.executeUpdate();
				return null;
			}
		});
		
	}

	@Override
	public List findPageByQuery(final String hql, final int offset, final int length)
			throws Exception {
		List list = getHibernateTemplate().executeFind(new HibernateCallback(){
			public Object doInHibernate(Session session) throws HibernateException,SQLException{
			Query query = session.createQuery(hql);
			query.setFirstResult(offset);
			query.setMaxResults(length);
			List list = query.list();
			return list;
			}
		});
		return list;
	}

	@Override
	public int getAllRowCount(String hql) throws Exception {
		return super.getHibernateTemplate().find(hql).size();
	}

	@Override
	public PageBean queryForPage(String hql, int pageSize, int page) throws Exception {
		int allRow = getAllRowCount(hql); // 总记录数
		int totalPage = PageBean.countTotalPage(pageSize, allRow); // 总页数
		int[] pageNumbers = new int[totalPage];
		for (int i = 0; i < totalPage; i++) {
			pageNumbers[i] = (i + 1) ; // 设置页码
		}
		final int offset = PageBean.countOffset(pageSize, page); // 当前页开始记录
		final int length = pageSize; // 每页记录数
		final int currentPage = PageBean.countCurrentPage(page);
		List list = findPageByQuery(hql, offset, length); //"一页"的记录
		PageBean pageBean = new PageBean();
		pageBean.setPageNumbers(pageNumbers);
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();
		return pageBean; 
	}
}
